package com.chijun.mform.modify;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.chijun.mform.FormBuilder;
import com.chijun.mform.R;
import com.chijun.mform.create.IWaitResult;
import com.chijun.mform.create.field.Field;
import com.chijun.mform.create.mode.FormGroup;
import com.example.ccj.b.c.dialog.ErrorDialog;
import com.example.ccj.u.JsonUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;

import java.util.List;

public class ModifyFormConfigMainControl extends LinearLayout {

    //表单基础配置 如组列表、表图像列表、日期类型，回调等
    FormBaseConfig formBaseConfig;
    FormData formData;


    public ModifyFormConfigMainControl(Context context) {
        super(context);
    }

    public ModifyFormConfigMainControl(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    //初始化界面
    private void initView(){
        inflate(getContext(), R.layout.control_modify_form_config,this);
        formBaseConfig = new FormBaseConfig();

    }

    private void loadBaseConfig(){
        formBaseConfig.modifyFormListener.getCreateFormConfig(new IWaitResult() {
            @Override
            public void onResult(String val) {
                JSONObject jsonObject = JSONObject.parseObject(val);
                //字段配置
                if (jsonObject.containsKey("field")) {
                    String fieldStr = jsonObject.getString("field");
                    if (!StringUtils.isEmpty(fieldStr)) {
                        List<Field> listBean = JsonUtils.toListBean(fieldStr, Field.class);
                        if (listBean != null) {
                            Field[] fields = new Field[listBean.size()];
                            for (int i = 0; i < listBean.size(); i++) {
                                fields[i] = listBean.get(i);
                            }
                            formBaseConfig.fields = fields;
                        } else {
                            LogUtils.w("未获取到字段配置！");
                        }
                    }
                }

                //表单类型配置
                if (jsonObject.containsKey("group")) {
                    String fieldStr = jsonObject.getString("group");
                    if (!StringUtils.isEmpty(fieldStr)) {
                        formBaseConfig.formGroupList = JsonUtils.toListBean(fieldStr, FormGroup.class);
                    } else {
                        LogUtils.w("未获取到表单分组配置！");
                    }
                }

                //日期格式配置
                if (jsonObject.containsKey("time")) {
                    String fieldStr = jsonObject.getString("time");
                    if (!StringUtils.isEmpty(fieldStr)) {
                        formBaseConfig.timeType = JsonUtils.toListBean(fieldStr, String.class);
                    }
                }
            }

            @Override
            public void onError(String error) {
                new ErrorDialog(getContext(),error).show();
                LogUtils.e(error);
            }
        });
    }

    //加载字段
    private void loadField(){

    }

    /**
     * 加载表单基础配置
     */
    private void loadBaseInformation (){

    }

    /**
     * 加载审批流程
     */
    private void loadProcess() {

    }




}
